<template>
    <div class="flex gap-4 mb-4">
      <span><b>用户搜索： </b></span>
      <el-input
        v-model="input2"
        style="width: 240px"
        placeholder="输入用户名或者电话"
        :prefix-icon="Search"
        @input="$emit('output',input2)"
      /> 
      <div class="spacer"></div>
      
    </div>
 
  <el-button  type="primary" @click="dialogFormVisible = true">
    添加用户
  </el-button>


   <el-dialog v-model="dialogFormVisible" title="添加用户" width="500">
    <el-form :model="form" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input v-model="form.username" autocomplete="off" @input = "checkForm"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账号">
            <el-input v-model="form.account" autocomplete="off" @input = "checkForm"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="密码">
            <el-input v-model="form.password" autocomplete="off" @input = "checkForm"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input v-model="form.phone" autocomplete="off" @input = "checkForm"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input v-model="form.email" autocomplete="off" @input = "checkForm"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-input v-model="form.sex" autocomplete="off" @input = "checkForm"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="生日">
            <el-date-picker
              v-model="form.birthday"
              type="date"
              placeholder="Pick a day"
            />
          </el-form-item>
        </el-col>
     
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item>
            <el-button  @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm"  :disabled="flag">添加</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>

 



</template>

<script lang="ts" setup>

import { ref } from 'vue'
import axios from 'axios'
  import { Calendar, Search } from '@element-plus/icons-vue'
  const input1 = ref('')
  const input2 = ref('')
  const input3 = ref('')
  const input4 = ref('')
  
import { reactive} from 'vue'
const flag = ref(true);
const dialogTableVisible = ref(false)
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'



const form = reactive({
  username: '',
  account: '',
  password: '',
  phone: '',
  email: '',
  sex: '',
  birthDay: '',
  creat_date:'',
  type: [],
  resource: '',
  desc: '',
})
function checkForm(){
  if(form.username && form.account && form.password && form.phone && form.email && form.sex){
      flag.value = false;
  }else{
    flag.value = true;
  }
}
import {  } from 'vue'
import { flattedChildren } from 'element-plus/es/utils';


const value1 = ref('')
const value2 = ref('')
const deleteSelected = ref(false)
const shortcuts = [
  {
    text: 'Today',
    value: new Date(),
  },
  {
    text: 'Yesterday',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24)
      return date
    },
  },
  {
    text: 'A week ago',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
      return date
    },
  },
]

const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}

  function submitForm(){
    const userData = {
                account: form.account,
                password: form.password,
                username: form.username,
                email: form.email,
                phone: form.phone,
                sex : form.sex,
                birthday: form.birthday
            }
            console.log(userData.data);
            axios.post('http://localhost:9001/UserAccount/regist',userData).then(response =>{
                if(response.data){
                    alert("添加成功")
                    this.$router.push('/')
                }else{
                    alert("添加失败，账号已存在")
                }})
                dialogFormVisible.value = false;
  }



</script>


  <style scoped>
  .spacer {
    margin: 16px 0; /* 控制空行的高度，16px 可以根据需要调整 */
  }

  .demo-date-picker {
  display: flex;
  width: 100%;
  padding: 0;
  flex-wrap: wrap;
}

.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}

.demo-date-picker .block:last-child {
  border-right: none;
}

.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}
  </style>
